﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Marking.Dashboard.Infrastructures;

namespace Marking.Dashboard.ViewModels.Sales
{
    public class CycleSearchViewModel : ViewModelBase
    {
        public CycleSearchViewModel()
        {
            this.CurrentCircleType = CurrentCircleType ?? this.CircleTypes.First();
        }


        public Action<CycleSearchViewModel> SearchInfoChanged { get; set; }

        public IEnumerable<string> CircleTypes
        {
            get { return CycleTypeHelper.CircleTypes; }
        }

        private string _currentCircleType;
        public string CurrentCircleType
        {
            get { return _currentCircleType; }
            set
            {
                _currentCircleType = value;

                this.ResetStartDateAndEndDate();
            }
        }

        private DateTime _startDate;
        public DateTime StartDate
        {
            get { return _startDate; }
            set
            {
                _startDate = value;

                this.NotifyPropertyChanged("StartDate");

                this.OnSearchInfoChanged();
            }
        }

        private DateTime _endDate;
        public DateTime EndDate
        {
            get { return _endDate; }
            set
            {
                _endDate = value;

                this.NotifyPropertyChanged("EndDate");

                this.OnSearchInfoChanged();
            }
        }

        private void ResetStartDateAndEndDate()
        {
            _endDate = DateTime.Now;

            this.StartDate = CycleTypeHelper.GetDefaultStartDate(this.CurrentCircleType, _endDate);
        }

        private void OnSearchInfoChanged()
        {
            if (this.StartDate == DateTime.MinValue || this.EndDate == DateTime.MinValue)
            {
                return;
            }

            if (this.StartDate >= this.EndDate)
            {
                return;
            }

            if (this.SearchInfoChanged != null)
            {
                this.SearchInfoChanged(this);
            }
        }
    }
}
